//
//  FGMessageCellData.m
//  FGChatKit
//
//  Created by Henry on 2020/8/13.
//  Copyright © 2020 Henry. All rights reserved.
//

#import "FGMessageCellData.h"
#import "UIColor+FGChat.h"
#import "FGResources.h"
#import "FGChatConfiguration.h"
#import "FGChatHelper.h"

@implementation FGMessageCellData

- (instancetype)initWithDirection:(FGMsgDirection)direction {
    self = [super init];
    if (self) {
        _direction = direction;
        [self defaultInit];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self defaultInit];
    }
    return self;
}

- (void)defaultInit {
    _status = FGMsgStatusInit;
    _uuid = [FGChatHelper generateUUID];
    _nameFont = [UIFont systemFontOfSize:13];
    _nameColor = [UIColor fg_colorWithLightColor:[UIColor fg_colorWithString:@"#666666"] darkColor:[UIColor fg_colorWithString:@"#DDDDDD"]];

    _timeFont = [UIFont systemFontOfSize:13];
    _timeColor = [UIColor fg_colorWithLightColor:[UIColor fg_colorWithString:@"#666666"] darkColor:[UIColor fg_colorWithString:@"#DDDDDD"]];

    _avatarImage = [FGResources getImageFromMessage:@"default_head"];
    
    _cellLayout = [[FGMessageCellLayout alloc] init];
    if (_direction == FGMsgDirectionOutgoing) {
        _name = [FGChatConfiguration shareInstance].loginName;
        _sender = [[FGChatConfiguration shareInstance] loginId];
    }
}

- (CGFloat)heightOfWidth:(CGFloat)width {
    CGFloat height = 0;
    
    if (self.showTime) {
        height = height + self.timeFont.lineHeight + self.cellLayout.topMargin;
    }
    
    if (self.showName && self.direction == FGMsgDirectionIncoming) {
        height += self.nameFont.lineHeight;
    }
    
    height += self.cellLayout.topMargin;
    height += self.cellLayout.bottomMargin;
        
    CGSize containerSize = [self contentSize];
    height += containerSize.height;
    
    return height;
}

- (CGSize)contentSize {
    return CGSizeZero;
}

+ (NSString *)reuseIdentifier {
    return @"FGMessageCell";
}

+ (void)registerCellToTableView:(UITableView *)tableView {
    NSString *reuseIdentifier = [[self class] reuseIdentifier];
    [tableView registerClass:NSClassFromString(reuseIdentifier) forCellReuseIdentifier:reuseIdentifier];
}

- (FGMessageCell *)dequeueCellByTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    return (FGMessageCell *)[tableView dequeueReusableCellWithIdentifier:[[self class] reuseIdentifier] forIndexPath:indexPath];
}

- (BOOL)isSelf {
    if (self.direction == FGMsgDirectionOutgoing) {
        return YES;
    }
    return NO;
}

@end
